package com.bigData.main.service.impl;

import com.bigData.main.mapper.UserMapper;
import com.bigData.main.pojo.UserInfo;
import com.bigData.main.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


@Service        //标注此类为业务实现层
public class UserServiceImpl implements UserService {

    @Resource       //从Spring容器中获取对象
    UserMapper umap;

    public Integer insertUserInfo(UserInfo u) {
        return umap.insertUser(u);
    }

    public Integer deleteUserInfo(UserInfo u) {
        return umap.deleteUser(u);
    }

    public Integer updateUserNameInfo(UserInfo u) {

        UserInfo user = umap.getUserName(u);
        //保存返回值
        int re = 0;
        if (user != null) {
            re = 2;
            //用户存在不可以注册
        } else {
            //可以注册
            re = umap.updateUsername(u);
        }
        return re;
    }

    public Integer updateUserPassInfo(UserInfo u) {
        return umap.updateUserPass(u);
    }

    public Integer updateUrootInfo(UserInfo u) {
        return umap.updateURoot(u);
    }

    public List<UserInfo> selectUserInfo(UserInfo u) {
        return umap.selectUser();
    }


    public UserInfo getUserPassWord(UserInfo u) {
        //调用根据用户名查密码
        UserInfo user = umap.getUserPass(u);
        //保存返回值
        int re = 0;
        if (user != null) {
            //用户存在
            String upass = user.getUpass();
            if (upass.equals(u.getUpass())) {
                //登录成功
                re = 2;
            } else {
                //用户名或密码错误
                re = 3;
            }
        } else {
            //用户不存在
            re = 4;
        }
        UserInfo p = new UserInfo();
        p.setRe(re);
        p.setUid(user.getUid());
        p.setUname(user.getUname());
        p.setUroot(user.getUroot());
        return p;
    }

    //注册判断用户名是否存在
    public Integer userRegister(UserInfo u) {
        //调用根据用户名查密码
        UserInfo user = umap.getUserName(u);
        //保存返回值
        int re = 0;
        if (user != null) {
            re = 2;
            //用户存在不可以注册

        } else {
            //可以注册
            re = umap.insertUser(u);
        }
        return re;
    }


}

